package com.bocloud.cmp.web.utils;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.web.model.Alarm;
import com.bocloud.cmp.web.model.CloudServer;
import com.bocloud.cmp.web.model.ExportData;
import com.bocloud.cmp.web.model.HostSystem;
import com.bocloud.cmp.web.model.OpenstackAbstract;
import com.bocloud.cmp.web.model.PlateformAbstract;
import com.bocloud.cmp.web.model.VmwareAbstract;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.DateTool;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 报表类
 * 
 * @author songsong
 * 
 *
 */
public class ReportForm {

	private static Logger logger = LoggerFactory.getLogger(ReportForm.class);

	public static BsmResult createPdf(BsmResult result, HttpServletResponse response) {
		try {
			ExportData data = new ExportData();
			JSONObject jsonObject = JSONObject.parseObject(result.getData().toString());
			JSONArray hosts = JSONArray.parseArray(jsonObject.get("hosts").toString());
			JSONArray alarms = JSONArray.parseArray(jsonObject.get("alarms").toString());
			Object plateFormObject = jsonObject.get("plateFormAbstract");
			List<HostSystem> hostList = new ArrayList<>();
			List<Alarm> alarmList = new ArrayList<>();
			for (Object host : hosts) {
				JSONObject hostResult = JSONObject.parseObject(host.toString());
				List<CloudServer> cloudServers = JSONArray.parseArray(hostResult.getString("servers"),
						CloudServer.class);
				hostResult.remove("servers");
				HostSystem hostSystem = JSONObject.parseObject(hostResult.toString(), HostSystem.class);
				hostSystem.setServers(cloudServers);

				hostList.add(hostSystem);
			}

			for (Object alarm : alarms) {
				Alarm alarm2 = JSONObject.parseObject(alarm.toString(), Alarm.class);
				alarmList.add(alarm2);
			}
			JSONObject plateJsonObject = JSONObject.parseObject(plateFormObject.toString());
			plateJsonObject.get("vmware");

			VmwareAbstract vmwareAbstract = null;
			OpenstackAbstract openstackAbstract = null;

			String pdfName = "";
			if ("VMWARE".equals(JSONObject.parseObject(result.getData().toString()).get("type").toString())) {
				vmwareAbstract = JSONObject.parseObject(plateJsonObject.get("vmware").toString(), VmwareAbstract.class);
				pdfName = "VMWARE平台";
			} else if ("OPENSTACK".equals(JSONObject.parseObject(result.getData().toString()).get("type").toString())) {
				openstackAbstract = JSONObject.parseObject(plateJsonObject.get("openstack").toString(),
						OpenstackAbstract.class);
				pdfName = "OPENSTACK平台";
			} else if ("ALL".equals(JSONObject.parseObject(result.getData().toString()).get("type").toString())) {
				vmwareAbstract = JSONObject.parseObject(plateJsonObject.get("vmware").toString(), VmwareAbstract.class);
				openstackAbstract = JSONObject.parseObject(plateJsonObject.get("openstack").toString(),
						OpenstackAbstract.class);
				pdfName = "纳管平台";
			}

			PlateformAbstract plateformAbstract = new PlateformAbstract();
			plateformAbstract.setVmware(vmwareAbstract);
			plateformAbstract.setOpenstack(openstackAbstract);

			data.setHosts(hostList);
			data.setAlarms(alarmList);
			data.setPlateFormAbstract(plateformAbstract);

			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			Document document = new Document();
			PdfWriter writer = PdfWriter.getInstance(document, baos);
			document.open();
			// 设置字体
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font chineseFont = new Font(bfChinese, 11, Font.BOLD);
			Font chineseFont8 = new Font(bfChinese, 8, Font.BOLD);
			Font chineseFont24 = new Font(bfChinese, 24, Font.BOLD);// 标题字体
			Paragraph praTitle = new Paragraph("云平台资源数据统计", chineseFont24);
			praTitle.setAlignment(1);// 标题居中
			document.add(praTitle);

			Paragraph blankRow1 = new Paragraph(18f, " ", chineseFont);// 空格
			document.add(blankRow1);// 添加空行

			if (data != null) {
				if (null != data.getPlateFormAbstract().getVmware()) {
					// 设置段落进行文字说明
					Paragraph paragraph = new Paragraph("VMWARE平台统计数据", chineseFont);
					paragraph.setAlignment(1);
					document.add(paragraph);
					document.add(blankRow1);// 添加空行
					PdfPTable table = new PdfPTable(12);// 表格有多少列
					table.setHeaderRows(1);// 每页都输出表头
					PdfPCell cell1 = new PdfPCell(new Paragraph("数据中心数量", chineseFont));
					PdfPCell cell2 = new PdfPCell(new Paragraph("集群数量", chineseFont));
					PdfPCell cell3 = new PdfPCell(new Paragraph("宿主机数量", chineseFont));
					PdfPCell cell4 = new PdfPCell(new Paragraph("虚拟机数量", chineseFont));
					PdfPCell cell5 = new PdfPCell(new Paragraph("模板数量", chineseFont));
					PdfPCell cell6 = new PdfPCell(new Paragraph("快照数量", chineseFont));
					PdfPCell cell7 = new PdfPCell(new Paragraph("网络数量", chineseFont));
					PdfPCell cell8 = new PdfPCell(new Paragraph("存储数量", chineseFont));
					PdfPCell cell9 = new PdfPCell(new Paragraph("cpu数量", chineseFont));
					PdfPCell cell10 = new PdfPCell(new Paragraph("内存大小(M)", chineseFont));
					PdfPCell cell11 = new PdfPCell(new Paragraph("磁盘大小(GB)", chineseFont));
					PdfPCell cell12 = new PdfPCell(new Paragraph("告警数量", chineseFont));
					// 水平居中
					cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell9.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell10.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell12.setHorizontalAlignment(Element.ALIGN_CENTER);

					table.addCell(cell1);
					table.addCell(cell2);
					table.addCell(cell3);
					table.addCell(cell4);
					table.addCell(cell5);
					table.addCell(cell6);
					table.addCell(cell7);
					table.addCell(cell8);
					table.addCell(cell9);
					table.addCell(cell10);
					table.addCell(cell11);
					table.addCell(cell12);

					PdfPCell cells1 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getCenters()), chineseFont8));
					PdfPCell cells2 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getClusters()), chineseFont8));
					PdfPCell cells3 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getHosts()), chineseFont8));
					PdfPCell cells4 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getServers()), chineseFont8));
					PdfPCell cells5 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getTemplates()), chineseFont8));
					PdfPCell cells6 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getSnapshots()), chineseFont8));
					PdfPCell cells7 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getNetworks()), chineseFont8));
					PdfPCell cells8 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getStores()), chineseFont8));
					PdfPCell cells9 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getCpus()), chineseFont8));
					PdfPCell cells10 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getMems()), chineseFont8));
					PdfPCell cells11 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getDisks()), chineseFont8));
					PdfPCell cells12 = new PdfPCell(new Paragraph(
							String.valueOf(data.getPlateFormAbstract().getVmware().getAlarms()), chineseFont8));

					// 水平居中
					cells1.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells2.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells3.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells4.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells5.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells6.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells7.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells8.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells9.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells10.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells11.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells12.setHorizontalAlignment(Element.ALIGN_CENTER);

					table.addCell(cells1);
					table.addCell(cells2);
					table.addCell(cells3);
					table.addCell(cells4);
					table.addCell(cells5);
					table.addCell(cells6);
					table.addCell(cells7);
					table.addCell(cells8);
					table.addCell(cells9);
					table.addCell(cells10);
					table.addCell(cells11);
					table.addCell(cells12);
					document.add(table);

				}

			}

			// 加入空行
			document.add(blankRow1);
			document.add(blankRow1);

			if (null != data.getPlateFormAbstract().getOpenstack()) {
				// 设置段落进行文字说明
				Paragraph paragraph = new Paragraph("OPENSTACK统计数据", chineseFont);
				paragraph.setAlignment(1);
				document.add(paragraph);
				document.add(blankRow1);// 添加空行
				PdfPTable table2 = new PdfPTable(11);// 表格有多少列
				table2.setHeaderRows(1);// 每页都输出表头
				PdfPCell cell1 = new PdfPCell(new Paragraph("宿主机数量", chineseFont));
				PdfPCell cell2 = new PdfPCell(new Paragraph("虚拟机数量", chineseFont));
				PdfPCell cell3 = new PdfPCell(new Paragraph("模板数量", chineseFont));
				PdfPCell cell4 = new PdfPCell(new Paragraph("快照数量", chineseFont));
				PdfPCell cell5 = new PdfPCell(new Paragraph("镜像数量", chineseFont));
				PdfPCell cell6 = new PdfPCell(new Paragraph("网络数量", chineseFont));
				PdfPCell cell7 = new PdfPCell(new Paragraph("存储数量", chineseFont));
				PdfPCell cell8 = new PdfPCell(new Paragraph("CPU数量", chineseFont));
				PdfPCell cell9 = new PdfPCell(new Paragraph("告警数量", chineseFont));
				PdfPCell cell10 = new PdfPCell(new Paragraph("内存大小(M)", chineseFont));
				PdfPCell cell11 = new PdfPCell(new Paragraph("磁盘大小(GB)", chineseFont));
				// 水平居中
				cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell9.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell10.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell11.setHorizontalAlignment(Element.ALIGN_CENTER);

				table2.addCell(cell1);
				table2.addCell(cell2);
				table2.addCell(cell3);
				table2.addCell(cell4);
				table2.addCell(cell5);
				table2.addCell(cell6);
				table2.addCell(cell7);
				table2.addCell(cell8);
				table2.addCell(cell9);
				table2.addCell(cell10);
				table2.addCell(cell11);

				PdfPCell cells1 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getHosts()), chineseFont8));
				PdfPCell cells2 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getServers()), chineseFont8));
				PdfPCell cells3 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getTemplates()), chineseFont8));
				PdfPCell cells4 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getSnapshots()), chineseFont8));
				PdfPCell cells5 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getImages()), chineseFont8));
				PdfPCell cells6 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getNetworks()), chineseFont8));
				PdfPCell cells7 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getVolumes()), chineseFont8));
				PdfPCell cells8 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getCpus()), chineseFont8));
				PdfPCell cells9 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getAlarms()), chineseFont8));
				PdfPCell cells10 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getMems()), chineseFont8));
				PdfPCell cells11 = new PdfPCell(new Paragraph(
						String.valueOf(data.getPlateFormAbstract().getOpenstack().getDisks()), chineseFont8));

				// 水平居中
				cells1.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells2.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells3.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells4.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells5.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells6.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells7.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells8.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells9.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells10.setHorizontalAlignment(Element.ALIGN_CENTER);
				cells11.setHorizontalAlignment(Element.ALIGN_CENTER);

				table2.addCell(cells1);
				table2.addCell(cells2);
				table2.addCell(cells3);
				table2.addCell(cells4);
				table2.addCell(cells5);
				table2.addCell(cells6);
				table2.addCell(cells7);
				table2.addCell(cells8);
				table2.addCell(cells9);
				table2.addCell(cells10);
				table2.addCell(cells11);
				document.add(table2);
			}

			// 物理机以及虚拟机报表
			List<HostSystem> hostSystems = data.getHosts();
			if (null != hostSystems) {
				// 设置段落进行文字说明
				Paragraph paragraph = new Paragraph("物理机统计数据", chineseFont);
				paragraph.setAlignment(1);
				document.add(paragraph);
				document.add(blankRow1);// 添加空行
				for (int i = 0; i < hostSystems.size(); i++) {
					document.add(blankRow1);// 添加空行
					PdfPTable hostTable = new PdfPTable(7);// 表格有多少列
					PdfPCell cell1 = new PdfPCell(new Paragraph("名称", chineseFont));
					PdfPCell cell2 = new PdfPCell(new Paragraph("IP", chineseFont));
					PdfPCell cell3 = new PdfPCell(new Paragraph("集群", chineseFont));
					PdfPCell cell4 = new PdfPCell(new Paragraph("电源状态", chineseFont));
					PdfPCell cell5 = new PdfPCell(new Paragraph("虚机总数", chineseFont));
					PdfPCell cell6 = new PdfPCell(new Paragraph("模板总数", chineseFont));
					PdfPCell cell7 = new PdfPCell(new Paragraph("所属平台", chineseFont));
					// 水平居中
					cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
					hostTable.addCell(cell1);
					hostTable.addCell(cell2);
					hostTable.addCell(cell3);
					hostTable.addCell(cell4);
					hostTable.addCell(cell5);
					hostTable.addCell(cell6);
					hostTable.addCell(cell7);

					PdfPCell cells1 = new PdfPCell(new Paragraph(hostSystems.get(i).getName(), chineseFont8));
					PdfPCell cells2 = new PdfPCell(new Paragraph(hostSystems.get(i).getHostIp(), chineseFont8));
					PdfPCell cells3 = new PdfPCell(new Paragraph(hostSystems.get(i).getClusterName(), chineseFont8));
					String powerState = "";
					if ("RUNNING".equals(hostSystems.get(i).getPowerState())) {
						powerState = "运行中";
					} else if ("STOPPED".equals(hostSystems.get(i).getPowerState())) {
						powerState = "关机";
					} else if ("UNKNOWN".equals(hostSystems.get(i).getPowerState())) {
						powerState = "已断开";
					}
					PdfPCell cells4 = new PdfPCell(new Paragraph(powerState, chineseFont8));
					String vmNum = hostSystems.get(i).getVmNum().toString();
					PdfPCell cells5 = new PdfPCell(new Paragraph(vmNum, chineseFont8));
					String templateNum = "";
					if (null != hostSystems.get(i).getTemplateNum()) {
						templateNum = hostSystems.get(i).getTemplateNum().toString();
					}
					PdfPCell cells6 = new PdfPCell(new Paragraph(templateNum, chineseFont8));
					PdfPCell cells7 = new PdfPCell(new Paragraph(hostSystems.get(i).getVendorName(), chineseFont8));

					// 水平居中
					cells1.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells2.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells3.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells4.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells5.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells6.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells7.setHorizontalAlignment(Element.ALIGN_CENTER);
					// 添加单元格
					hostTable.addCell(cells1);
					hostTable.addCell(cells2);
					hostTable.addCell(cells3);
					hostTable.addCell(cells4);
					hostTable.addCell(cells5);
					hostTable.addCell(cells6);
					hostTable.addCell(cells7);
					// 添加表格
					document.add(hostTable);

					List<CloudServer> servers = hostSystems.get(i).getServers();
					// 生成虚拟机表格：这些虚拟机是相应物理机的虚拟机
					if (null != servers && servers.size() > 0) {
						PdfPTable serverTable = new PdfPTable(7);// 表格有多少列
						// 要显示的虚拟机的熟悉
						PdfPCell serverCell1 = new PdfPCell(new Paragraph("虚机名称", chineseFont));
						PdfPCell serverCell2 = new PdfPCell(new Paragraph("宿主机", chineseFont));
						PdfPCell serverCell3 = new PdfPCell(new Paragraph("IP地址", chineseFont));
						PdfPCell serverCell4 = new PdfPCell(new Paragraph("状态", chineseFont));
						PdfPCell serverCell5 = new PdfPCell(new Paragraph("数据存储区", chineseFont));
						PdfPCell serverCell6 = new PdfPCell(new Paragraph("快照数量", chineseFont));
						PdfPCell serverCell7 = new PdfPCell(new Paragraph("磁盘/GB", chineseFont));
						// 水平居中
						serverCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
						serverCell2.setHorizontalAlignment(Element.ALIGN_CENTER);
						serverCell3.setHorizontalAlignment(Element.ALIGN_CENTER);
						serverCell4.setHorizontalAlignment(Element.ALIGN_CENTER);
						serverCell5.setHorizontalAlignment(Element.ALIGN_CENTER);
						serverCell6.setHorizontalAlignment(Element.ALIGN_CENTER);
						serverCell7.setHorizontalAlignment(Element.ALIGN_CENTER);
						serverTable.addCell(serverCell1);
						serverTable.addCell(serverCell2);
						serverTable.addCell(serverCell3);
						serverTable.addCell(serverCell4);
						serverTable.addCell(serverCell5);
						serverTable.addCell(serverCell6);
						serverTable.addCell(serverCell7);

						for (int j = 0; j < servers.size(); j++) {
							PdfPCell serverCells1 = new PdfPCell(new Paragraph(servers.get(j).getName(), chineseFont8));
							PdfPCell serverCells2 = new PdfPCell(
									new Paragraph(servers.get(j).getHostName(), chineseFont8));
							PdfPCell serverCells3 = new PdfPCell(
									new Paragraph(servers.get(j).getManagerIp(), chineseFont8));
							String status = "";
							if ("RUNNING".equals(servers.get(j).getStatus())) {
								status = "运行中";
							} else if ("STOPPED".equals(servers.get(j).getStatus())) {
								status = "关机";
							} else {
								status = "异常";
							}
							PdfPCell serverCells4 = new PdfPCell(new Paragraph(status, chineseFont8));
							PdfPCell serverCells5 = new PdfPCell(
									new Paragraph(servers.get(j).getDataStore(), chineseFont8));
							String snapShotNum = "";
							if (null != servers.get(j).getSnapshotCount()) {
								snapShotNum = servers.get(j).getSnapshotCount().toString();
							}
							PdfPCell serverCells6 = new PdfPCell(new Paragraph(snapShotNum, chineseFont8));
							String diskNum = "";
							if (null != servers.get(j).getDisk()) {
								diskNum = servers.get(j).getDisk().toString();
							}
							PdfPCell serverCells7 = new PdfPCell(new Paragraph(diskNum, chineseFont8));
							// 水平居中
							serverCells1.setHorizontalAlignment(Element.ALIGN_CENTER);
							serverCells2.setHorizontalAlignment(Element.ALIGN_CENTER);
							serverCells3.setHorizontalAlignment(Element.ALIGN_CENTER);
							serverCells4.setHorizontalAlignment(Element.ALIGN_CENTER);
							serverCells5.setHorizontalAlignment(Element.ALIGN_CENTER);
							serverCells6.setHorizontalAlignment(Element.ALIGN_CENTER);
							serverCells7.setHorizontalAlignment(Element.ALIGN_CENTER);
							// 添加虚拟机对应属性的值
							serverTable.addCell(serverCells1);
							serverTable.addCell(serverCells2);
							serverTable.addCell(serverCells3);
							serverTable.addCell(serverCells4);
							serverTable.addCell(serverCells5);
							serverTable.addCell(serverCells6);
							serverTable.addCell(serverCells7);
						}
						document.add(serverTable);
					}
				}
			}

			document.add(blankRow1);// 添加空
			document.add(blankRow1);// 添加空
			// 设置段落进行文字说明
			Paragraph paragraph = new Paragraph("告警统计数据", chineseFont);
			paragraph.setAlignment(1);
			document.add(paragraph);
			document.add(blankRow1);// 添加空

			List<Alarm> alarmss = data.getAlarms();
			if (null != alarmss) {
				// 创建告警表格
				PdfPTable tables = new PdfPTable(8);// 表格有多少列
				PdfPCell cell1 = new PdfPCell(new Paragraph("告警名称", chineseFont));
				PdfPCell cell2 = new PdfPCell(new Paragraph("告警类型", chineseFont));
				PdfPCell cell3 = new PdfPCell(new Paragraph("告警目标", chineseFont));
				PdfPCell cell4 = new PdfPCell(new Paragraph("告警状态", chineseFont));
				PdfPCell cell5 = new PdfPCell(new Paragraph("触发时间", chineseFont));
				PdfPCell cell6 = new PdfPCell(new Paragraph("确认时间", chineseFont));
				PdfPCell cell7 = new PdfPCell(new Paragraph("确认人", chineseFont));
				PdfPCell cell8 = new PdfPCell(new Paragraph("所属平台", chineseFont));
				// 水平居中
				cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell8.setHorizontalAlignment(Element.ALIGN_CENTER);

				// 添加告警属性
				tables.addCell(cell1);
				tables.addCell(cell2);
				tables.addCell(cell3);
				tables.addCell(cell4);
				tables.addCell(cell5);
				tables.addCell(cell6);
				tables.addCell(cell7);
				tables.addCell(cell8);

				for (int i = 0; i < alarmss.size(); i++) {
					PdfPCell cells1 = new PdfPCell(new Paragraph(alarmss.get(i).getName(), chineseFont8));
					PdfPCell cells2 = new PdfPCell(new Paragraph(alarmss.get(i).getType(), chineseFont8));
					PdfPCell cells3 = new PdfPCell(new Paragraph(alarmss.get(i).getTarget(), chineseFont8));
					String confirm = "";
					if ("CONFIRMED".equals(alarmss.get(i).getStatus())) {
						confirm = "已确认";
					} else if ("UNCONFIRM".equals(alarmss.get(i).getStatus())) {
						confirm = "未确认";
					}
					PdfPCell cells4 = new PdfPCell(new Paragraph(confirm, chineseFont8));
					String gmtTrigger = "";
					if (null != alarmss.get(i).getGmtTrigger()) {
						gmtTrigger = DateTool.formatTime(alarmss.get(i).getGmtTrigger());
					}
					PdfPCell cells5 = new PdfPCell(new Paragraph(gmtTrigger, chineseFont8));
					String gmtConfirm = "";
					if (null != alarmss.get(i).getGmtConfirm()) {
						gmtConfirm = DateTool.formatTime(alarmss.get(i).getGmtConfirm());
					}
					PdfPCell cells6 = new PdfPCell(new Paragraph(gmtConfirm, chineseFont8));
					String confirmer = alarmss.get(i).getConfirmer();
					PdfPCell cells7 = new PdfPCell(new Paragraph(confirmer, chineseFont8));
					String vendorName = alarmss.get(i).getVendorName();
					PdfPCell cells8 = new PdfPCell(new Paragraph(vendorName, chineseFont8));
					// 水平居中
					cells1.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells2.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells3.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells4.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells5.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells6.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells7.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells8.setHorizontalAlignment(Element.ALIGN_CENTER);
					// 添加告警属性对应的值
					tables.addCell(cells1);
					tables.addCell(cells2);
					tables.addCell(cells3);
					tables.addCell(cells4);
					tables.addCell(cells5);
					tables.addCell(cells6);
					tables.addCell(cells7);
					tables.addCell(cells8);
				}
				document.add(tables);
			}

			document.close();
			writer.close();
			// 下载表格到浏览器
			byte[] content = baos.toByteArray();
			response.setCharacterEncoding("utf-8");
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition", "attachment;fileName="
					+ URLEncoder.encode(new String(pdfName + new Date().getTime() + ".pdf"), "UTF-8"));
			OutputStream os = response.getOutputStream();
			os.write(content);
			os.close();
			os.flush();
		} catch (Exception e) {
			logger.error("生成文档失败", e);
			return new BsmResult(false, "发生异常生成文档失败");
		}
		return new BsmResult(true, "生成文档成功");
	}
}
